package com.echo.yixiaowu.feature.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.zIndex
import androidx.media3.session.MediaController
import androidx.navigation.NavController
import com.echo.yixiaowu.data.model.player.CardDetailItemData
import com.echo.yixiaowu.feature.player.MediaPageIntent
import com.echo.yixiaowu.feature.player.MediaUIState
import com.echo.yixiaowu.util.formatTimeString

@Composable
fun LandscapeVideoPlayer(
    navController: NavController,
    itemData: CardDetailItemData.VideoItem,
    resetHideControlTimer: () -> Unit,
    isAutoRotateEnabled: Boolean,
    mediaState: MediaUIState,
    mediaController: MediaController,
    player: @Composable (Modifier) -> Unit,
    onIntent: (MediaPageIntent) -> Unit
) {
    val aspectRatio = mediaState.width.toFloat() / mediaState.height
    val videoModifier = Modifier
        .fillMaxHeight()
        .aspectRatio(aspectRatio)
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black),
    ) {
        VideoControl(
            modifier = Modifier
                .fillMaxWidth()
                .zIndex(99f),
            onToggleFullScreen = {
                resetHideControlTimer()
                onIntent(
                    MediaPageIntent.ClickMediaFullScreen(
                        mediaState.isFullScreen, mediaState.originalVideoHeight
                    )
                )
            },
            bufferProgress = mediaState.bufferProgress,
            progress = mediaState.progress,
            videoDurationString = mediaState.totalDuration.formatTimeString(),
            currentDurationString = mediaState.currentDuration.formatTimeString(),
            totalDuration = mediaState.totalDuration,
            currentDuration = mediaState.currentDuration,
            onShowControls = {
                resetHideControlTimer()
                onIntent(MediaPageIntent.ClickShowControls(mediaState.isShowControl))
            },
            onPlayOrPause = {
                resetHideControlTimer()
                onIntent(MediaPageIntent.ClickPlaying(mediaState.isPlaying))
            },
            isFullScreen = mediaState.isFullScreen,
            isShowControl = mediaState.isShowControl,
            resetHideControlTimer = resetHideControlTimer,
            onProgressChange = {
                resetHideControlTimer()
                onIntent(MediaPageIntent.SeekToProgress(it))
            },
            isPlaying = mediaState.isPlaying
        ) {
            player(videoModifier)
        }
    }
}